package com.wangyadong.hobby.javanetwork.address;

import java.io.IOException;
import java.net.*;
import java.util.Arrays;
import java.util.Enumeration;

/**
 * <p>
 *
 * @author <714037465@qq.com>
 * @since 2019/7/99:12
 **/
public class InetAddressDemo {

    public static void main(String[] args) throws IOException {
        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        while (interfaces.hasMoreElements()) {
            NetworkInterface networkInterface = interfaces.nextElement();
            Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
            while (addresses.hasMoreElements()) {
                InetAddress address = addresses.nextElement();
                System.out.println(address);
            }
        }
        System.out.println("======================================");
        NetworkInterface eth3 = NetworkInterface.getByName("eth3");
        Enumeration<InetAddress> inetAddresses = eth3.getInetAddresses();
        while (inetAddresses.hasMoreElements()){
            InetAddress address = inetAddresses.nextElement();
            System.out.println(address);
        }
        ///
        System.out.println("======================================");
        InetAddress inetAddress = InetAddress.getLocalHost();
        NetworkInterface byInetAddress = NetworkInterface.getByInetAddress(inetAddress);
        Enumeration<InetAddress> inetAddresses1 = byInetAddress.getInetAddresses();
        while (inetAddresses1.hasMoreElements()){
            InetAddress address = inetAddresses1.nextElement();
            System.out.println(address);
        }
    }


}
